package mul.UI;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;


public class AThread extends Thread{
	
	private SurfaceHolder surfaceHolder; //Surface holder that can access the physical surface
    private MainGamePanel gamePanel; //The actual view that handles inputs and draws to the surface
    private Canvas canvas;
    private boolean running; //flag to hold game state
	public void setRunning(boolean running) {
		this.running = running;
	}

	public AThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
		super();
		this.surfaceHolder = surfaceHolder;
		this.gamePanel = gamePanel;
	}
	
	@Override
	public void run() {
		while(running){
	        canvas = null;
	        // try locking the canvas for exclusive pixel editing
	        // in the surface
	        try {
	            canvas = this.surfaceHolder.lockCanvas();
	            synchronized (surfaceHolder) {
	                this.gamePanel.update(); //update game state
	                this.gamePanel.render(canvas); //render state to the screen,  draws the canvas on the panel
	            }
	        } 
	        finally {
	        	// in case of an exception the surface is not left in an inconsistent state
	            if (canvas != null) {
	            	surfaceHolder.unlockCanvasAndPost(canvas);
	            }
	        }
		}
	}
}
